<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['types']=array(
	'1'=>'普通防伪码',
	'2'=>'分节防伪码',
	'3'=>'防伪二维码',
);
if($_RQ['op']=='list'){
	pdo_update('jy_yxgoodserpfw_code',array('xiang_id'=>0),array('xiang_id >'=>0,'log_status'=>0,'uniacid'=>$_SESSION['uniacid']));
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['goods_id']){
		$params['goods_id']=$_RQ['goods_id'];
		$return['params']['goods_id']=$_RQ['goods_id'];
	}
	if($_RQ['is2']){
		$params['is2']=$_RQ['is2'];
		$return['params']['is2']=$_RQ['is2'];
	}
	//批号
	if($_RQ['code']){
		$params['code like']='%'.trim($_RQ['code']).'%';
		$return['params']['code']=$_RQ['code'];
	}
	if($_RQ['xcode']){
		$xiang_id=pdo_getcolumn('jy_yxgoodserpfw_xcode',array('code'=>$_RQ['xcode'],'uniacid'=>$_SESSION['uniacid']),'id');
		$params['xiang_id']=$xiang_id?:-1;
		$return['params']['xcode']=$_RQ['xcode'];
	}
	//备注
	if(!empty($_RQ['sn'])){
		$_RQ['sn']=str_replace('FW','',$_RQ['sn']);
		$_RQ['sn']=str_replace('XW','',$_RQ['sn']);
		$params['sn']=intval($_RQ['sn']);
		$return['params']['sn']=$_RQ['sn'];
	}
    $fields=array();
    $orderby='id desc';
	$total='';
	$list=pdo_getslice('jy_yxgoodserpfw_code',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$item){
		$item['beinum']=$item['endnum']-$item['startnum'];
		$item['sn']=sprintf("%08d", $item['sn']);
		$item['startnum']=sprintf("%08d", $item['startnum']);
		$item['endnum']=sprintf("%08d", $item['endnum']);
		$item['pre']=pdo_getcolumn('jy_yxgoodserpfw_batch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['batch_id']),'pre');
		$item['presn']=$item['pre'].$item['sn'];
		if($item['xiang_id']){
			$item['xsn']=pdo_getcolumn('jy_yxgoodserpfw_xcode',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['xiang_id']),'sn');
			$item['xsn']=sprintf("%08d", $item['xsn']);
		}
		$kulog=pdo_get('jy_yxgoodserpfw_kulog',array('code'=>$item['code'],'uniacid'=>$_SESSION['uniacid']),'','createtime desc');
		$item['kutime']=$kulog['createtime'];
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['status']=$this->code_status;
	$return['log_status']=$this->codelog_status;			
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'箱码管理'),
			array('op'=>'up2','title'=>'刷新溯源码'),
			array('op'=>'pici','title'=>'处理批次'),
		),
		'list'=>array(
		'tip'=>'防伪的<em>业务逻辑</em>：',
		'data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'pre','title'=>'前缀','type'=>'string'),
			array('name'=>'sn','title'=>'流水号','type'=>'string'),
			array('name'=>'presn','title'=>'带前缀流水号','type'=>'string'),
			array('name'=>'xsn','title'=>'装箱流水号','type'=>'string'),
			array('name'=>'code','title'=>'防伪码','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'log_status','title'=>'流通状态','type'=>'radio','options'=>'log_status'),
			array('name'=>'kutime','title'=>'最近流通时间','type'=>'time'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),
		),
		'search'=>array(			
			array('name'=>'sn','width'=>15,'title'=>'流水号','type'=>'string'),
			array('name'=>'code','width'=>15,'title'=>'防伪码','type'=>'string'),
			array('name'=>'xcode','width'=>15,'title'=>'箱标码','type'=>'string'),
			array('name'=>'goods_id','width'=>15,'title'=>'所属商品','type'=>'choose','action'=>'jy_yxgoodserpfw/get_goods/'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='up2'){
	$item['total']=pdo_count('jy_yxgoodserpfw_code',array('uniacid'=>$_SESSION['uniacid']));
	$item['is2']=pdo_count('jy_yxgoodserpfw_code',array('is2 >'=>1,'uniacid'=>$_SESSION['uniacid']));
	$item['is0']=pdo_count('jy_yxgoodserpfw_code',array('is2'=>0,'uniacid'=>$_SESSION['uniacid']));
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'同步操作',
		'edit'=>array(
			array('title'=>'[温馨提示]：<red>出现异常数据时，可以点击处理</red>','type'=>'tip'),
			array('name'=>'total','title'=>'码数量','type'=>'copy'),
			array('name'=>'batch_id','title'=>'批次','type'=>'select','options'=>'batchs','option_title'=>'code'),
			array('name'=>'is0','title'=>'待更新数量','type'=>'copy'),
			array('name'=>'is2','title'=>'处理数量','type'=>'copy'),
		)
	);
	$return['batchs']=pdo_getall('jy_yxgoodserpfw_batch',array('uniacid'=>$_SESSION['uniacid']),array('id','code'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$pindex = max(1, intval($_RQ['page']));
	$psize = 500;
	$params=array('uniacid'=>$_SESSION['uniacid'],'is2'=>0);
	if($postdata['batch_id']){
		$_RQ['batch_id']=$postdata['batch_id'];
	}
	if(!$_RQ['batch_id']){
		$is0=pdo_count('jy_yxgoodserpfw_code',array('is2'=>0,'uniacid'=>$_SESSION['uniacid']));
		if(!$is0){
			exi('同步成功','','/code/list/');
		}
		$orderby='id desc';
		$batchs=pdo_getall('jy_yxgoodserpfw_batch',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']),'id','',$orderby);
		foreach($batchs as $item){
			$item['is0']=pdo_count('jy_yxgoodserpfw_code',array('batch_id'=>$item['id'],'is2'=>0,'uniacid'=>$_SESSION['uniacid']));
			if($item['is0']){
				$_RQ['batch_id']=$item['id'];
				break;
			}
		}
		if(!$_RQ['batch_id']){
			exi('同步成功','','/code/list/');
		}
	}
	if($_RQ['batch_id']){
		$params['batch_id']=$_RQ['batch_id'];
		$return['params']['batch_id']=$_RQ['batch_id'];
	}
    $fields=array('id','code');
    $orderby='id desc';
	$total='';
	$list=pdo_getslice('jy_yxgoodserpfw_code',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as $item){
		$params['code']=$item['code'];
		$is2=pdo_count('jy_yxgoodserpfw_code',$params);
		if($is2>=2){
			pdo_update('jy_yxgoodserpfw_code',array('is2'=>$is2),$params);
		}else{
			pdo_update('jy_yxgoodserpfw_code',array('is2'=>1),$params);
		}
	}
	if(count($list)>450){
		$page=$pindex+1;
		exi('同步下一批500个盒码！','','/code/post/batch_id='.$_RQ['batch_id']);
	}
	exi('同步下一批次','','/code/post/');
}
if($_RQ['op']=='pici'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 25;
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']);
	if($goods_id){
		$params['goods_id']=$goods_id;
		$return['params']['goods_id']=$goods_id;
	}
	//批号
	if(!empty($_RQ['code'])){
		$params['code']=intval($_RQ['code']);
		$return['params']['code']=$_RQ['code'];
	}
	//备注
	if(!empty($_RQ['remark'])){
		$params['remark']='%'.trim($_RQ['remark']).'%';
		$return['params']['remark']=$_RQ['remark'];
	}
    $fields=array('id','goods_id','goods_code','code','num','xnum','startxn','endxn','startnum','endnum','type','createtime','status','remark');
    $orderby='id desc';
	$total='';
	$list=pdo_getslice('jy_yxgoodserpfw_batch',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$item){
		$item['total']=pdo_count('jy_yxgoodserpfw_code',array('batch_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
		$item['is1']=pdo_count('jy_yxgoodserpfw_code',array('batch_id'=>$item['id'],'is2'=>1,'uniacid'=>$_SESSION['uniacid']));
		$item['is2']=pdo_count('jy_yxgoodserpfw_code',array('batch_id'=>$item['id'],'is2 >'=>1,'uniacid'=>$_SESSION['uniacid']));
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['status']=array('1'=>'正常','0'=>'失效');		
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'list'=>array(
		'data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'code','title'=>'批号','type'=>'string'),
			array('name'=>'total','title'=>'总数量','type'=>'string'),
			array('name'=>'is1','title'=>'已数量','type'=>'string'),
			array('name'=>'is2','title'=>'处理数量','type'=>'string'),
		),
		),
		'search'=>array(			
			array('name'=>'goods_id','width'=>15,'title'=>'所属商品ID','type'=>'choose','op'=>'choose_goods'),
			array('name'=>'code','width'=>15,'title'=>'批号','type'=>'string'),
			array('name'=>'remark','width'=>15,'title'=>'备注','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
?>